<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>会员等级列表</title>
		<link rel="stylesheet" href="__PEAR__/component/pear/css/pear.css" />
		<style>
		.layui-input{width:130px;}
		.layui-form-label{width:auto;}
		.layui-form-item .layui-input-inline{width: 130px;}
		.layui-table-cell{height:auto !important}
		</style>
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="memberlevel-table" lay-filter="memberlevel-table"></table>
			</div>
		</div>

		<script type="text/html" id="memberlevel-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		        新增
		    </button>
		    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-delete"></i>
		        删除
		    </button>
		</script>
		
		<script type="text/html" id="memberlevel-bar">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>

		<script src="__STATIC__/lib/layui/layui.js"></script>
		<script src="__PEAR__/component/pear/pear.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery','common'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;
		  	
		  		let MODULE_PATH = "/{$MODULE_NAME}/member/";

				let cols = [
					[{
							type: 'checkbox'
						},
						{
							title: '编号',
							field: 'lid',
							align: 'center',
							width: 100
						},
						{
							title: '等级名称',
							field: 'levelname',
							align: 'center'
						},
						{
							title: '级别代码',
							field: 'code',
							align: 'center'
						},
						{
							title: '代码名称',
							field: 'code_name',
							align: 'center'
						},
						{
							title: '折扣',
							field: 'discount',
							align: 'center'
						},
						{
							title: '优惠',
							field: 'sale',
							align: 'center',
						},
						{
							title: '操作',
							toolbar: '#memberlevel-bar',
							align: 'center',
							width: 130
						}
					]
				]

				table.render({
					elem: '#memberlevel-table',
					url: MODULE_PATH+"levelList",
					page: true,
					cols: cols,
					skin: 'line',
					toolbar: '#memberlevel-toolbar',
					defaultToolbar: []
				});

				table.on('tool(memberlevel-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(memberlevel-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});

				form.on('submit(memberlevel-query)', function(data) {
					table.reload('memberlevel-table', {
						where: data.field,
						page:{curr:1}
					})
					return false;
				});
				
				window.add=function(){
					window.edit(null);
				}
				
				window.edit = function(obj) {
					var lid=0;
					var _title='添加会员等级';
					if(obj!=null){
						var lid=obj.data.lid;
							_title="编辑会员等级";
					}
					
					layer.open({
						type: 2,
						title: _title,
						shade: 0.1,
						area: ['600px', '400px'],
						content: MODULE_PATH + 'levelEdit/lid/'+lid+'.html'
					});
				}

				window.remove = function(obj) {
					layer.confirm('确定要删除该会员等级?', {
						icon: 3,
						title: '删除提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: MODULE_PATH + "delLevel/lid/" + obj.data['lid'],
							dataType: 'json',
							type: 'delete',
							success: function(result) {
								layer.close(loading);
								if (result.code) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										obj.del();
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}

				window.batchRemove = function(obj) {
					
					var checkIds = common.checkField(obj,'lid');
					
					if (checkIds === "") {
						layer.msg("未选中数据", {
							icon: 3,
							time: 1000
						});
						return false;
					}
					
					layer.confirm('确定要删除这些会员等级?', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: MODULE_PATH + "delLevel/lid/" + checkIds,
							dataType: 'json',
							type: 'delete',
							success: function(result) {
								layer.close(loading);
								if (result.code) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										table.reload('memberlevel-table');
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}

				window.refresh = function(param) {
					table.reload('memberlevel-table');
				}
			})
		</script>
	</body>
</html>
